package views;

import utils.LoginUtils;
import utils.MessageUtils;
import utils.NavigatorUtils;
import utils.SessionUtils;
import views.components.Header;
import views.components.LangSelector;

import com.vaadin.annotations.Theme;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import config.Parameters;

@SuppressWarnings("serial")
@Theme("Theme02")
/**
 * Clase que es utilitza per a fer login
 *
 */
public class LoginView extends CustomComponent implements View,
		Button.ClickListener {

	private TextField tfUser;
	private PasswordField tfPassword;
	private Button btLogin;
	private LangSelector langSelector;
	private VerticalLayout root, contenedorCabecera, contenedorFields;
	private String username, password, profile;
	private Header header;

	public LoginView() {
		iniciarVariables();
		estils();
	}

	private void estils() {

		root.setMargin(true);
		root.setSpacing(true);
		contenedorCabecera.setMargin(true);
		contenedorCabecera.setSpacing(true);
		contenedorFields.setSpacing(true);
		contenedorFields.setMargin(true);

		contenedorFields.setSizeUndefined();
		langSelector.setSizeUndefined();
		contenedorCabecera.setComponentAlignment(langSelector,
				Alignment.TOP_CENTER);
		root.setComponentAlignment(contenedorFields, Alignment.TOP_CENTER);
		setCompositionRoot(root);
	}

	private void iniciarVariables() {

		tfUser = new TextField(MessageUtils.getI18n("user"));
		tfPassword = new PasswordField(MessageUtils.getI18n("password"));
		header = new Header(MessageUtils.getI18n("login_to_system"));
		langSelector = new LangSelector(Parameters.LOGIN_VIEW_NAME);
		btLogin = new Button(MessageUtils.getI18n("login"), this);
		contenedorCabecera = new VerticalLayout(header, langSelector);
		contenedorFields = new VerticalLayout(tfUser, tfPassword, btLogin);
		root = new VerticalLayout(contenedorCabecera, contenedorFields);
		tfUser.setValue("eduard");
		tfPassword.setValue("informatica01");
	}

	@Override
	public void buttonClick(ClickEvent event) {

		if (event.getComponent().equals(btLogin)) {

			// get field values

			username = tfUser.getValue();
			password = tfPassword.getValue();

			// search for errors

			try {
				profile = LoginUtils
						.validarUserNamePassword(username, password);

				// Store the current user in the service session
				// set list of all available applications
				SessionUtils.setSessionUserAndApllications(username, profile);

				// Navigate to main view
				NavigatorUtils.navigateTo(getUI().getNavigator(),
						Parameters.MAIN_VIEW_NAME);

			} catch (Exception e) {
				Notification.show(MessageUtils.getI18n(e.getMessage()), Type.HUMANIZED_MESSAGE);
				e.printStackTrace();
			}

		}
	}

	/**
	 * focus the username field when user arrives to the login view
	 */
	@Override
	public void enter(ViewChangeEvent event) {
		tfUser.focus();
	}

}
